package com.csu.bailan.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csu.bailan.common.CommonResponse;
import com.csu.bailan.common.ResponseEnum;
import com.csu.bailan.domain.SKU;
import com.csu.bailan.domain.SPU;
import com.csu.bailan.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequestMapping("/goods/")
@Slf4j
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("info")
    public CommonResponse<JSONArray> selectSPU(@RequestParam String spuId){
        JSONArray j = goodsService.getAttributesBySPUid(spuId);
        if (j != null)
            return CommonResponse.createForSuccess(goodsService.getAttributesBySPUid(spuId));
        return CommonResponse.createForError(ResponseEnum.SPUNotExisted.getCode(), ResponseEnum.SPUNotExisted.getMessage());
    }

    /**
     *
     * attributes要按照顺序传入，且在前端用URLEncoder用UTF-8编码
     * @param attributes
     * @param spuId
     * @return
     */
    @GetMapping("getStock")
    public CommonResponse<Integer> getSKUStock(@RequestParam String attributes, @RequestParam String spuId){
        String attrs = URLDecoder.decode(attributes, StandardCharsets.UTF_8);
        SKU sku = goodsService.getSKUByAttrAndSPUid(spuId, attributes);
        if (sku != null)
            return CommonResponse.createForSuccess(sku.getStock());
        return CommonResponse.createForError(ResponseEnum.StockQueryError.getCode(), ResponseEnum.StockQueryError.getMessage());
    }

    @GetMapping("pageSelectByC")
    public CommonResponse<JSONObject> getDisplayByCategory(String c_id, int page, double priceGt, double priceLt){
        List<SPU> list = goodsService.selectPageByCID(c_id, page, 10, priceGt, priceLt);
        if (list.isEmpty())
            return CommonResponse.createForError(ResponseEnum.PageQueryError.getCode(), ResponseEnum.PageQueryError.getMessage());
        JSONObject jsonObject = new JSONObject(true);
        jsonObject.put("total", list.size());
        jsonObject.put("goods", list);
        return CommonResponse.createForSuccess(jsonObject);
    }

    @GetMapping("allGoods")
    public CommonResponse<JSONObject> getDisplay(int page, double priceGt, double priceLt){
        List<SPU> list = goodsService.selectPage(page, 10, priceGt, priceLt);
        if (list.isEmpty())
            return CommonResponse.createForError(ResponseEnum.PageQueryError.getCode(), ResponseEnum.PageQueryError.getMessage());
        JSONObject jsonObject = new JSONObject(true);
        jsonObject.put("total", list.size());
        jsonObject.put("goods", list);
        return CommonResponse.createForSuccess(jsonObject);
    }


}
